"""
Case Type   : 兼容日期函数
Case Name   : date_add(date, interval exper unit)函数第一参数输入time类型参数测试边界值，第二参数输入正确
Create At   : 2023.03.15
Owner       :@zou_jialiang0501129488
Description :
    1、创建兼容b库并查询插件是否加载成功
    2、date_add(date, interval exper unit)函数第一参数输入time类型参数测试边界值，第二参数输入正确
    3、清理环境
Expect      :
    1、成功，插件查询成功
    2、成功
    3、清理成功
History     :modified by zhanghuan96，2024.8.26，IAHP3M问题修复，代码变更
"""

import os
import unittest

from yat.test import Node
from yat.test import macro

from testcase.utils.Common import Common
from testcase.utils.CommonSH import CommonSH
from testcase.utils.Constant import Constant
from testcase.utils.Logger import Logger


class CompatibilityTest01(unittest.TestCase):

    def setUp(self):
        self.log = Logger()
        self.log.info(f'-----{os.path.basename(__file__)} start-----')
        self.common = Common()
        self.sh_primary = CommonSH('PrimaryDbUser')
        self.constant = Constant()
        self.db_name = 'db_date_add_0024'
        self.sql_name = 'sql_file_0024.sql'

    def test_create_function(self):
        text = '-----step1:创建兼容b库库并查询插件;expect:成功-----'
        self.log.info(text)
        sql_cmd = f"drop database if exists {self.db_name};" \
                  f"create database {self.db_name} dbcompatibility ='B';"
        self.log.info(sql_cmd)
        sql_res = self.sh_primary.execut_db_sql(sql_cmd)
        self.log.info(sql_res)
        self.assertIn(self.constant.CREATE_DATABASE_SUCCESS,
                      sql_res, '兼容B库创建失败')

        sql_cmd1 = "select extname from pg_extension where extname ='dolphin';"
        self.log.info(sql_cmd1)
        sql_res1 = self.sh_primary.execut_db_sql(sql_cmd1, dbname=self.db_name)
        self.log.info(sql_res1)
        self.assertIn('dolphin', sql_res1, '查询失败')

        text = '-----step2:date_add(date, interval)第一参数输入time类型参数边界值，' \
               '第二参数输入正确;expect:成功-----'
        self.log.info(text)
        sql_cmd2 = f"""set dolphin.support_interval_to =on;
                select date_add(time'838:59:59', interval -1 day);
                select date_add(time'838:59:59', interval -2 hour);
                select date_add(time'838:59:59', interval -3 minute);
                select date_add(time'838:59:59', interval -4 second);
                select date_add(time'-838:59:59', interval 5 day);
                select date_add(time'-838:59:59', interval 6 hour);
                select date_add(time'-838:59:59', interval 7 minute);
                select date_add(time'-838:59:59', interval 8 second);
                select date_add(time'838:59:59', interval -9 day to hour);
                select date_add(time'-838:59:59', interval 10 day to minute);
                select date_add(time'838:59:59', interval -11 day to second);
                select date_add(time'-838:59:59', interval 12 hour to minute);
                select date_add(time'838:59:59', interval -13 hour to second);
                select date_add(time'-838:59:59', interval 14 minute to second);
                """
        self.log.info(sql_cmd2)
        echo_cmd = f"rm -rf {self.sql_name};" \
                   f"echo \"\"\"{sql_cmd2}\"\"\" > {self.sql_name};" \
                   f"ls | grep {self.sql_name}"
        self.log.info(echo_cmd)
        res = self.common.get_sh_result(self.sh_primary.node, echo_cmd)
        self.assertIn(self.sql_name, res, '文件写入失败')

        sql_cmd3 = f'source {macro.DB_ENV_PATH};gsql -d {self.db_name} ' \
                   f'-p {self.sh_primary.node.db_port} ' \
                   f'-f {self.sql_name}'
        self.log.info(sql_cmd3)
        sql_res3 = self.common.get_sh_result(self.sh_primary.node, sql_cmd3)
        self.log.info(sql_res3)
        msg_lis = ['814:59:59', '836:59:59', '838:56:59', '838:59:55',
                   '-718:59:59', '-832:59:59', '-838:52:59', '-838:59:51',
                   '829:59:59', '-838:49:59', '838:59:48', '-838:47:59',
                   '838:59:46', '-838:59:45']
        for msg in msg_lis:
            self.assertIn(msg, sql_res3, '函数调用失败')

    def tearDown(self):
        text = '-----step3:清理环境;expect:成功-----'
        self.log.info(text)
        sql_cmd4 = f"drop database if exists {self.db_name};"
        self.log.info(sql_cmd4)
        sql_res4 = self.sh_primary.execut_db_sql(sql_cmd4)
        self.log.info(sql_res4)
        del_cmd = f'rm -rf {self.sql_name};' \
                  f'if [ -e {self.sql_name} ]; ' \
                  f'then echo "exists"; else echo "not exists"; fi'
        self.log.info(del_cmd)
        del_res = self.common.get_sh_result(self.sh_primary.node, del_cmd)
        self.assertIn(self.constant.DROP_DATABASE_SUCCESS,
                      sql_res4, '清理环境失败')
        self.assertIn('not exists', del_res, '文件删除失败')
        self.log.info(f'-----{os.path.basename(__file__)} end-----')
